package cn.bblocks.cacheTest.suport.dto.response;
import java.io.Serializable;
import java.util.List;

import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.bblocks.cacheTest.configuration.PortListener;
import cn.bblocks.cacheTest.constant.Constant;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * @author xiaomai
 */
@ApiModel(value = "返回参数")
@Getter
@Setter
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 6686419048438176898L;
    
    
    
    /**
     * 状态码
     */

    @ApiModelProperty(value = "错误码,0=成功，其他为错")
    private Integer error;

    
    @ApiModelProperty(value = "错误信息")
    private String message ;
    
    @ApiModelProperty(value="id")
    private Long id;
    /**
     * 业务数据
     */
    @ApiModelProperty(value = "数据")

    private T body;
    
    @ApiModelProperty(value = "登录专用数据Token")
    private String token;
    
    @ApiModelProperty(value = "主机(信息)")
    private String serverInfo;

    public Response<T> error(int error) {
        this.error = error;
        return this;
    }

    public Response<T> id(Long id){
        this.id=id;
        return this;
    }
    public Response<T> body(T body) {
        this.body = body;
        return this;
    }


    private Response() {
//    	PortListener portListener = DynamicBeanManager.getBean(PortListener.class);
    	PortListener portListener =(PortListener) Constant.appContext.getBean("portListener");
    	serverInfo = portListener.getSerInfo();
    }

    public Response(int status, String message, T body) {
    	this();
        this.error = status;
        this.message = message;
        this.body = body;
    }


    public static <T> Response getInstance(T body) {
        return new Response<T>(0, null, body);
    }

    public static Response ok() {
        return new Response().error(0).message("成功");
    }

    public static Response nonExpect(int error) {
        return new Response().error(error);
    }


    public Boolean hasError() {
        return this.error != 0;
    }

    @Override
    public String toString() {
        if (null == this.body) {
            this.body((T) new JSONObject());
        }
        return JSON.toJSONString(this);
    }

    public Response message(String message) {
        this.message = message;
        return this;
    }
}
